local containers = require("containers")
local params = containers.params

-- 给容器对象添加一个名为 abigail 的容器，用的是坎普斯背包的配置修改的
params.player = {
    widget = {
        slotpos = {},
        animbank = "ui_chest_3x3", -- 建议自定义大背景动画，否则会显示异常
        animbuild = "ui_chest_3x3",
        pos = Vector3(-300, 0, 0),   
        --side_align_tip = 160,
        enable_Page = true,
        page_grid_num = 49,
        max_page = 3,
        page_text_x = -167 / 2 - 40,
        page_text_y = -360,
        page_btn_x = -167,
        page_btn_spacing = 75,
        page_btn_y = -360,
        page_btn_x_scale = .7,
        page_btn_y_scale = .7,
    },
    usespecificslotsforitems = true,--是否翻页
    issidewidget = false,--是否贴边
    type = "chest",
    openlimit = 1,
    itemtestfn = function(inst, item, slot)
        return not item.replica.container
    end
}

for page = 1, params.player.widget.max_page do
    for i = 1, params.player.widget.page_grid_num do
        -- 这里以每页7x6为例，实际布局可自定义
        local row = math.floor((i-1)/7)
        local col = (i-1)%7
        
        -- 如果是第一页，保持原有的y坐标计算
        -- 如果是后面的页面，y坐标与第一页的相同
        local y_offset = (page == 1) and (-75 * row + 200 + 30) or (-75 * row + 200 + 30)
        
        table.insert(params.player.widget.slotpos, Vector3(75 * col - 225, y_offset, 0))
    end
end

params.player.widget.OnMouseButton = function(widget, button, down, x, y)
    -- 只有按住Ctrl+鼠标右键才允许拖拽
    if button == MOUSEBUTTON_RIGHT and down and TheInput:IsKeyDown(KEY_CTRL) then
        print("开始拖拽")
        widget.draging = true
        widget:FollowMouse()
    elseif button == MOUSEBUTTON_RIGHT then
        widget.draging = false
        widget:StopFollowMouse()
        print("退出拖拽")
    end
end

for k, v in pairs(params) do
    containers.MAXITEMSLOTS = math.max(containers.MAXITEMSLOTS, v.widget.slotpos ~= nil and #v.widget.slotpos or 0)
end
-- 容器打开时回调
local function onopen(inst)
    inst.SoundEmitter:PlaySound("dontstarve/wilson/chest_open")
end

-- 容器关闭时回调
local function onclose(inst)
    inst.SoundEmitter:PlaySound("dontstarve/wilson/chest_close")
end
--注册组件和函数
local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    MakeInventoryPhysics(inst)
    inst.entity:AddNetwork()
    inst.entity:AddSoundEmitter()
    ---------------------------------------------------------------------------主机
    if not TheWorld.ismastersim then
        return inst
    end
    -- 添加容器组件
    inst:AddComponent("container")
    inst.components.container:EnableInfiniteStackSize(true) -- 启用无限堆叠
    -- 设置容器名
    inst.components.container:WidgetSetup("player")
    inst.components.container.onopenfn = onopen
    inst.components.container.onclosefn = onclose

    inst:AddComponent("inventoryitem") -- 必须有这个组件才能被装备
    inst.components.inventoryitem.cangoincontainer = false
    
    inst:AddComponent('equippable')
    inst.components.equippable.equipslot = EQUIPSLOTS.PLAYER
    return inst
end

return Prefab('player_container', fn, prefabs)